<?php

namespace App\Admin\Controllers\User;

use App\Admin\Actions\Post\BatchRestore;
use App\Admin\Actions\Post\Restore;
use App\Admin\Repositories\User\CmcMember;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CmcMemberController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CmcMember(), function (Grid $grid) {
            $grid->filter(function ($filter) {
                // 范围过滤器，调用模型的`onlyTrashed`方法，查询出被软删除的数据。
                $filter->scope('trashed', '回收站')->onlyTrashed();
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if (request('_scope_') == 'trashed') {
                    $actions->append(new Restore(\App\Models\user\CmcMember::class));
                }
            });

            $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
                if (request('_scope_') == 'trashed') {
                    $batch->add(new BatchRestore(\App\Models\user\CmcMember::class));
                }
            });
            $grid->column('member_id')->sortable();
            $grid->column('username');
            $grid->column('nickname');
            $grid->column('phone');

            $grid->column('avatar')->image($server = null, 120, 100);

            $grid->column('reg_time');
            $grid->column('point');
            $grid->column('balance');

            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('member_id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CmcMember(), function (Show $show) {
            $show->field('member_id');
            $show->field('username');
            $show->field('nickname');
            $show->field('phone');
            $show->field('password');
            $show->field('status');
            $show->field('avatar');
            $show->field('qq_openid');
            $show->field('wx_openid');
            $show->field('wb_openid');
            $show->field('login_ip');
            $show->field('last_login_ip');
            $show->field('reg_time');
            $show->field('point');
            $show->field('balance');
            $show->field('pay_password');
            $show->field('last_login_time');
            $show->field('sex');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CmcMember(), function (Form $form) {
            $form->display('member_id');
            $form->text('username');
            $form->text('nickname');
            $form->text('phone');
            $form->text('status');
            $form->image('avatar');
            $form->text('reg_time');
            $form->text('point');
            $form->text('balance');
            $form->text('last_login_time');
            $form->text('sex');
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
